import axios from "axios"
import store from "../store"
import { Toast } from "antd-mobile"
const request = axios.create({
    baseURL: "http://127.0.0.1:8089",
    // headers: {
    //     Accept: 'application/json, text/plain, */*',
    //     Host: 'httpbin.org',
    //     'User-Agent': 'axios/0.26.1',
    //     'X-Amzn-Trace-Id': ''
    //   },
})

request.interceptors.request.use(
    function (config: any) {
        let state: any = store.getState()
        let tooken = state.user.tooken
        if (config.method.toLocaleLowerCase() === "get") {
            config.params = { ...config.params, tooken }
        } else {
            config.data = { ...config.data, tooken }
        }
        return config
    },
    function (err) {
        return Promise.reject(err)
    }
)

request.interceptors.response.use(
    function (res) {
        //关闭动画
        return res;
    },
    function (err) {
        // 失败
        // http的状态吗
        //Do something with response error
        // console.log(err,'error')
        let status = err.message.split(" ").reverse()[0];

        // console.log(status, "status");
        if (status == 301) {
            // message.error("访问地址有问题返回404");
            Toast.show({
                icon: 'fail',
                content: '访问受限,需要登陆',
            })
        }
        // 404
        // 405
        // console.log(err.message, "wo cuo le");
        return Promise.reject(err);
    }
);

export default request